/*
 * Forward.java
 *
 * Version information
 *
 * Apr 13, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.ilink.webapp.struts.action;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.ilink.hibernate.Redirect;
import net.amsoft.iservice.ilink.service.ILinkService;
import net.amsoft.iservice.ilink.util.ILinkConstants;
import net.amsoft.iservice.ilink.webapp.dataObject.ILink;
import net.amsoft.iservice.ilink.webapp.struts.form.ConfigForm;
import net.amsoft.iservice.util.exception.IServiceException;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.apache.log4j.Logger;

/**
 * This forward action class is used to test created I-Link by clicking on any
 * of the links will take you to the website that you had defined to be the
 * Forwards To address (URL) for a tag
 * 
 * @author mohit
 * 
 */
public class Forward extends Action {
	private static final Logger oLogger = Logger.getLogger(Forward.class);

	public final ActionForward execute(ActionMapping oMapping,
			ActionForm oForm, HttpServletRequest oRequest,
			HttpServletResponse oResponse) {
		oLogger.info("execute(): entry");
		String sSynonym = null;
		String sTag = null;

		try {
			String sQxri = oRequest.getParameter("xri").toLowerCase();
			if (sQxri.startsWith("xri://"))
				sQxri = sQxri.substring("xri://".length());

			sSynonym = sQxri.substring(0, sQxri.indexOf("/"));
			sTag = sQxri.substring(sSynonym.length() + 1);
			//TODO : if tag starts and ends with brackets it removes them and the resulting tag is not valid.	
			if (sTag.startsWith("(") && sTag.endsWith(")")) {
				sTag = sTag.substring(1, sTag.length() - 1);
			}

			if (sTag.charAt(0) == ' ') {
				sTag = sTag.trim();
				sTag = '+' + sTag;
			}
			oLogger.debug("execute() : Data : QXRI: " + sQxri + ", Synonym: "
					+ ", Tag: " + sTag);
		} catch (Exception e) {
			oLogger.warn("execute() : Exception: " + e.getMessage());
			try {
				oResponse.sendError(HttpServletResponse.SC_NOT_FOUND,
						"Invalid Request");
				oLogger.warn("execute() : Exception, return null");
				return null;
			} catch (IOException e1) {
				oLogger.warn("execute() : Exception: " + e1.getMessage());
			}
		}

		// display public tags if the requested tag is "+index"
		if (!ILinkService.isPublicDefined(sSynonym, ILinkConstants.PUBLIC_TAG)
				&& sTag.equalsIgnoreCase(ILinkConstants.PUBLIC_TAG)) {
			oLogger.info("execute() : " + ILinkConstants.PUBLIC_TAG
					+ " tag is recieved and 0is not created by" + sSynonym);
			ConfigForm oConfigForm = new ConfigForm();
			List oRedirectList = ILinkService.getPublicRedirects(sSynonym);
			ILink[] oILink = new ILink[oRedirectList.size()];
			Redirect oRedirect = null;
			int i = 0;
			for (Iterator oIter = oRedirectList.iterator(); oIter.hasNext();) {
				oRedirect = (Redirect) oIter.next();
				String sCustomLink = ILinkConstants.URL_XRI_NET + "/"
						+ oRedirect.getSynonym().getSynonym() + "/" + "(" + oRedirect.getTag() + ")";
				oILink[i++] = new ILink(sCustomLink,
						oRedirect.getWebResource(), oRedirect.getPublic());
			}
			oConfigForm.setIlink(oILink);
			oRequest.setAttribute(ILinkConstants.REQUEST_CONFIG_OBJ,
					oConfigForm);
			return oMapping.findForward(ILinkConstants.FWD_PUBLIC);
		}

		try {
			String sWebResource = ILinkService.getWebresourceAddress(sSynonym,
					sTag);
			if (sWebResource == null) {
				oLogger
						.warn("execute() : Display 404 error page, WebResource: "
								+ sWebResource);
				oResponse
						.sendError(404,
								"Incorrect request or Requested resource could not be resolved");
				return null;
			} else
				return new ActionForward(sWebResource, true);
		} catch (IServiceException e) {
			oLogger.warn("execute() : Exception: " + e.getMessage());
		} catch (IOException e1) {
			oLogger.warn("execute() : Exception: " + e1.getMessage());
		}
		oLogger.warn("execute() : exit, return null");
		return null;
	}
}
